package com.hdjd.controller;

import com.hdjd.entities.*;
import com.hdjd.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘子强
 * 2021/1/25 17:25
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //根据商家id查询该商家所有商品集合及商品数量
    @ResponseBody
    @RequestMapping(value = "/selectGoodsListByAccId", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsListByAccId(Integer accId, Integer goStatus, String goNm, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<Goods> goodsList;
        Integer count;
        try {
            goodsList = this.goodsService.selectGoodsListByAccId(accId, goStatus, goNm, pageCurrent, pageSize);
            count = this.goodsService.countGoodsNumByAccId(accId,goStatus,goNm);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!goodsList.isEmpty()){
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", goodsList);
            map.put("count", count);
            return map;
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
            return map;
        }
    }

    //新增商品
    @ResponseBody
    @RequestMapping(value = "/insertGoods", method = RequestMethod.POST)
    public Map<String, Object> insertGoods(Goods goods){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.insertGoods(goods);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "新增失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "新增成功");
            map.put("orderDetails", goods);
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "新增失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "selectGoodsListByGoNmOrSelectAll",method = RequestMethod.GET)
    public Map<String, Object> selectGoodsListByGoNmOrSelectAll(Integer accId,String goNm,Integer pageCurrent, Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<SellNum> goodsList = null;
        Integer count;
        try{
            goodsList = this.goodsService.selectGoodsListByGoNmOrSelectAll(accId,goNm, pageCurrent, pageSize);
            count = this.goodsService.selectAllGoodsNum(goNm,accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询所有商品失败或者根据商品名查询所有失败");
            return map;
        }
        if (goodsList != null){
            map.put("code", 1001);
            map.put("msg", "查询所有商品成功或者根据商品名模糊查询所有商品成功");
            map.put("goodsList", goodsList);
            map.put("goodsCount",count);
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "查询所有商品失败或者根据商品名查询所有失败");
            return map;
        }
    }
    //更新商品信息
    @ResponseBody
    @RequestMapping(value = "/updateGoodsInfoByGoId", method = RequestMethod.PUT)
    public Map<String, Object> updateGoodsInfoByGoId(Goods goods){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.updateGoodsByGoId(goods);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "更新失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "更新成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "更新失败");
            return map;
        }
    }
    //根据商品id查询商品信息
    @ResponseBody
    @RequestMapping(value = "/selectGoodsByGoId", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsByGoId(Integer goId){
        Map<String, Object> map = new HashMap<>();

        Goods goods = null;
        try {
            goods = this.goodsService.selectGoodsByGoId(goId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(goods != null){
            map.put("code", 1001);
            map.put("msg", "查询成功");
            map.put("goods", goods);
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
    }
    //商品上架与下架
    @ResponseBody
    @RequestMapping(value = "/OnShelfAndOffShelfGoods", method = RequestMethod.PUT)
    public Map<String, Object> OnShelfAndOffShelfGoods(Integer goId, Integer goStatus){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.OnShelfAndOffShelfGoodsByGoId(goId, goStatus);
        }catch (Exception e){
            e.printStackTrace();
            if(goStatus == 1){
                map.put("code", 1002);
                map.put("msg", "商品上架失败");
            }else if(goStatus == 2){
                map.put("code", 1001);
                map.put("msg", "商品下架失败");
            }
            return map;
        }
        if(flag){
            if(goStatus == 1){
                map.put("code", 1001);
                map.put("msg", "商品上架成功");
            }else if(goStatus == 2){
                map.put("code", 1001);
                map.put("msg", "商品下架成功");
            }
            return map;
        }else{
            if(goStatus == 1){
                map.put("code", 1002);
                map.put("msg", "商品上架失败");
            }else if(goStatus == 2){
                map.put("code", 1001);
                map.put("msg", "商品下架失败");
            }
            return map;
        }
    }

    //商品进货，插入记录
    @ResponseBody
    @RequestMapping(value = "/inGoods", method = RequestMethod.POST)
    public Map<String, Object> inGoods(GoodsIn goodsIn, Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.insertGoodsIn(goodsIn, accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "进货失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "进货成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "进货失败");
            return map;
        }
    }
    //商家进货后确认收货 （修改进货状态 为 1）
    @ResponseBody
    @RequestMapping(value = "/businessReceiveGoods", method = RequestMethod.PUT)
    public Map<String, Object> businessReceiveGoods(Integer inId){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.businessReceiveGoods(inId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "收货失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "收货成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "收货失败");
            return map;
        }
    }

    //商品退货，插入记录，并修改进货状态为 2
    @ResponseBody
    @RequestMapping(value = "/returnGoods", method = RequestMethod.POST)
    public Map<String, Object> returnGoods(Integer inId, Integer retNum, Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.insertGoodsReturn(inId, retNum, accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "退货失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "退货成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "退货失败");
            return map;
        }
    }

    //上传商品图片
    @ResponseBody
    @RequestMapping(value = "/insertGoodsImg", method = RequestMethod.POST)
    public Map<String, Object> insertGoodsImg(Integer goId, String imgIds, String imgDefault, @RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.insertGoodsImg(goId, imgIds, imgDefault, file, request);

        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "图片上传失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "图片上传成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "图片上传失败");
            return map;
        }
    }

    //上传评价图片
    @ResponseBody
    @RequestMapping(value = "/insertOpinionImg", method = RequestMethod.POST)
    public Map<String, Object> insertOpinionImg(Integer opiId, @RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.goodsService.insertOpinionImg(opiId, file, request);

        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "图片上传失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "图片上传成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "图片上传失败");
            return map;
        }
    }


    //查询商品的图片
    @ResponseBody
    @RequestMapping(value = "/selectGoodsImgUrl", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsImgUrl(Integer goId){
        Map<String, Object> map = new HashMap<>();
        List<String> imgUrl = null;
        try{
            imgUrl = this.goodsService.selectGoodsImgUrl(goId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询商品图片失败");
            return map;
        }
        if (imgUrl != null){
            map.put("code", 1001);
            map.put("msg", "查询商品图片成功");
            map.put("imgUrl",imgUrl);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "查询商品图片失败,没有商品图片");
            return map;
        }
    }

    //根据商品Id或者根据商品Id和商品评价类型查询商品评价信息
    @ResponseBody
    @RequestMapping(value = "/selectOpinionInfoByGoId",method = RequestMethod.GET)
    public Map<String, Object> selectOpinionInfoByGoId(Integer goId,Integer opiType,Integer pageCurrent,Integer pageSize){
        Map<String, Object> map = new HashMap<>();
        List<Opinion> opinions = null;
        int opinionNum;
        try {
            opinions = this.goodsService.selectOpinionInfoByGoId(goId,opiType,pageCurrent,pageSize);
            opinionNum = this.goodsService.selectOpinionNumByGoId(goId, opiType);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询商品评价失败");
            return map;
        }
        if (opinions != null){
            map.put("code", 1001);
            map.put("msg", "查询商品评价成功");
            map.put("opinions",opinions);
            map.put("opinionNum",opinionNum);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "没有评价");
            return map;
        }
    }
    //根据商品id查询商家所有进货记录（也可根据某段日期查询）及进货数
    @ResponseBody
    @RequestMapping(value = "/selectGoodsInListByAccId", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsInListByAccId(Integer accId, String startDate, String endDate, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<GoodsIn> goodsInList;
        Integer count;
        try {
            goodsInList = this.goodsService.selectGoodsInListByAccId(accId, startDate, endDate, pageCurrent, pageSize);
            count = this.goodsService.countGoodsInNumByAccId(accId, startDate, endDate);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!goodsInList.isEmpty()){
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", goodsInList);
            map.put("count", count);
            return map;
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
            return map;
        }
    }

    //根据商品id查询商家所有退货记录（也可根据某段日期查询）及记录数
    @ResponseBody
    @RequestMapping(value = "/selectGoodsReturnListByAccId", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsReturnListByAccId(Integer accId, String startDate, String endDate, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<GoodsReturn> goodsReturnList;
        Integer count;
        try {
            goodsReturnList = this.goodsService.selectGoodsReturnListByAccId(accId, startDate, endDate, pageCurrent, pageSize);
            count = this.goodsService.countGoodsReturnNumByAccId(accId, startDate, endDate);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!goodsReturnList.isEmpty()){
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", goodsReturnList);
            map.put("count", count);
            return map;
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/buyGoodsByAccIdAndGoId", method = RequestMethod.PUT)
    public Map<String, Object> buyGoodsByAccIdAndGoId(Integer accId,String goIdString){
        Map<String, Object> map = new HashMap<>();
        String[] goIds = goIdString.split(",");
        Integer ordId;
        try{
            ordId = this.goodsService.buyGoodsByAccIdAndGoId(accId,goIds);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "下单失败失败");
            return map;
        }
        if (ordId != null){
            map.put("code", 1001);
            map.put("msg", "下单成功");
            map.put("ordId", ordId);
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "下单失败失败");
            return map;
        }
    }

    //根据商品id 查询其图片集合
    @ResponseBody
    @RequestMapping(value = "/selectGoodsImgListByGoId", method = RequestMethod.GET)
    public Map<String, Object> selectGoodsImgListByGoId(Integer goId){
        Map<String, Object> map = new HashMap<>();

        List<GoodsImg> goodsImgList;
        try {
            goodsImgList = this.goodsService.selectGoodsImgListByGoId(goId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!goodsImgList.isEmpty()){
            map.put("code", 1001);
            map.put("msg", "查询成功");
            map.put("goodsImgList", goodsImgList);
            return map;
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
            return map;
        }
    }

}
